package com.hzya.frame.web.listener;

import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

public class SessionListener implements HttpSessionListener, HttpSessionAttributeListener {
	@SuppressWarnings("unused")
	private boolean flag = false;

	public void sessionCreated(HttpSessionEvent se) {
		flag = false;
		// System.out.println("=========开始监听SESSION==========");
	}

	// 当tomcat释放session或者session.invalidate()时，
	public void sessionDestroyed(HttpSessionEvent se) {
		flag = true;
	}

	// 下面是HttpSessionAttributeListener实现的方法，浏览器关闭的时候，调用attributeRemoved
	public void attributeAdded(HttpSessionBindingEvent arg0) {
		if (!"struts.token".equals(arg0.getName())) {
			flag = false;
			System.out.println(arg0.getName() + "====attributeAdded====");
		}
	}

	public void attributeRemoved(HttpSessionBindingEvent arg0) {
		if (!"struts.token".equals(arg0.getName())) {
			System.out.println(arg0.getName() + "====attributeRemoved====");
		}
	}

	public void attributeReplaced(HttpSessionBindingEvent arg0) {
		if (!"struts.token".equals(arg0.getName())) {
			flag = false;
		}
	}
}
